package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    @Autowired
    private TbBusinessMapper tbBusinessMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Override
    public List<TbBusinessTrackRecord> list(Long id) {
        return tbBusinessTrackRecordMapper.list(id);

    }

    @Override
    @Transactional
    public int add(BusinessTrackVo businessTrackVo) {

        Integer integer=0;
        try {
            TbBusiness tbBusiness = new TbBusiness();
            BeanUtils.copyProperties(businessTrackVo,tbBusiness);
            tbBusiness.setId(businessTrackVo.getBusinessId());

            TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
            BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);
            tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
            tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());

            CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
                return tbBusinessMapper.updateTbBusiness(tbBusiness);
            });

            CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
                return  tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);
            });
            //3 join等待所有线程全部执行完成
            CompletableFuture.allOf(future1,future2).join();
            integer = future1.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
            return 0;
        }
        return integer;
    }

    @Override
    public void addTbBusiness(Long id,String reson) {
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();

        List<SysDictData> sysDictDatas = sysDictDataMapper.selectDictDataByType("reasons_for_business_reporting");
        for (SysDictData dictData : sysDictDatas) {
                if(dictData.getDictValue().equals(reson)){
                    tbBusinessTrackRecord.setRecord(dictData.getDictLabel());
                }
            }

        tbBusinessTrackRecord.setBusinessId(id);
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecord.setTrackStatus(reson);

        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);

    }
}
